package com.example.eventbustest

import android.os.Handler
import android.os.Looper

class MainThreadPoster : Poster {
    companion object {
        val instance: MainThreadPoster by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) {
            MainThreadPoster()
        }
    }

    private val handler: Handler = Handler(Looper.getMainLooper())

    override fun post(item: MySubscription, event: Any) {
        handler.post {
            invokeMethod(item, event)
        }
    }

    private fun invokeMethod(item: MySubscription, event: Any) {
        item.method.method.invoke(item.subscribe, event)
    }
}